<template>
  <Card title="7天收运重量统计" :loading="loading">
    <!-- <VisitAnalysisBar /> -->
    <div ref="chartRef" :style="{ height, width }"></div>
  </Card>
</template>
<script lang="ts" setup>
  import { Ref, ref, watch } from 'vue';
  import { Card } from 'ant-design-vue';
  import { useECharts } from '/@/hooks/web/useECharts';
  // import VisitAnalysisBar from './VisitAnalysisBar.vue';
  const chartRef = ref<HTMLDivElement | null>(null);
  const { setOptions } = useECharts(chartRef as Ref<HTMLDivElement>);

  const props = defineProps({
    loading: Boolean,
    width: {
      type: String as PropType<string>,
      default: '100%',
    },
    height: {
      type: String as PropType<string>,
      default: '280px',
    },
    res:{
      type: Array,
      default: {
        data:{}
      },
    }
  });
 
  async function handleData() {
    // const res = await getShQtyCensus();
    let ddateArr = []
    let grzlArr = []
    let sszlArr = []
    props.res.data.forEach(item=>{
      ddateArr.push(item.ddate)
      grzlArr.push(item.grzl.toFixed(2))
      sszlArr.push(item.sszl.toFixed(2))
    })
    setOptions({
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          lineStyle: {
            width: 1,
            color: '#019680',
          },
        },
      },
      legend: {
        data: ['感染性', '损伤性']
      },
      grid: { left: '1%', right: '1%', top: '20  %', bottom: 0, containLabel: true },
      xAxis: {
        type: 'category',
        data:ddateArr,
      },
      yAxis: {
        type: 'value',
        max: 5000,
        splitNumber: 4,
        name: "单位  （kg）",
      },
      series: [
        {
          name: '感染性',
          data: grzlArr,
          type: 'bar',
          barMaxWidth: 80,
          label:{
            show:true,
            position:'top',
            // formatter:function(data){
            //   return data.value+'件'
            // }
          }
          // itemStyle: {
          //     color: 'red'
          // }
        },
        {
          name: '损伤性',
          data: sszlArr,
          type: 'bar',
          barMaxWidth: 80,
          label:{
            show:true,
            position:'top',
            // formatter:function(data){
            //   return data.value+'件'
            // }
          }
          // itemStyle: {
          //     color: 'yellow'
          // }
        },
      ],
    });
  }
  watch(
    () => [props.loading,props.res],
    () => {
      if (props.loading) {
        return;
      }
      handleData()
    },
    { immediate: true },
  );
</script>
